home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP15 / POPFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.6 KB  |  133 lines

  1. /*------------------------------------------
  2.    POPFILE.C -- Popup Editor File Functions
  3.   ------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <commdlg.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. static OPENFILENAME ofn ;
  11.  
  12. void PopFileInitialize (HWND hwnd)
  13.      {
  14.      static char szFilter[] = "Text Files (*.TXT)\0*.txt\0"  \
  15.                               "ASCII Files (*.ASC)\0*.asc\0" \
  16.                               "All Files (*.*)\0*.*\0\0" ;
  17.  
  18.      ofn.lStructSize       = sizeof (OPENFILENAME) ;
  19.      ofn.hwndOwner         = hwnd ;
  20.      ofn.hInstance         = NULL ;
  21.      ofn.lpstrFilter       = szFilter ;
  22.      ofn.lpstrCustomFilter = NULL ;
  23.      ofn.nMaxCustFilter    = 0 ;
  24.      ofn.nFilterIndex      = 0 ;
  25.      ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
  26.      ofn.nMaxFile          = _MAX_PATH ;
  27.      ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
  28.      ofn.nMaxFileTitle     = _MAX_FNAME + _MAX_EXT ;
  29.      ofn.lpstrInitialDir   = NULL ;
  30.      ofn.lpstrTitle        = NULL ;
  31.      ofn.Flags             = 0 ;             // Set in Open and Close functions
  32.      ofn.nFileOffset       = 0 ;
  33.      ofn.nFileExtension    = 0 ;
  34.      ofn.lpstrDefExt       = "txt" ;
  35.      ofn.lCustData         = 0L ;
  36.      ofn.lpfnHook          = NULL ;
  37.      ofn.lpTemplateName    = NULL ;
  38.      }
  39.  
  40. BOOL PopFileOpenDlg (HWND hwnd, PSTR pstrFileName, PSTR pstrTitleName)
  41.      {
  42.      ofn.hwndOwner         = hwnd ;
  43.      ofn.lpstrFile         = pstrFileName ;
  44.      ofn.lpstrFileTitle    = pstrTitleName ;
  45.      ofn.Flags             = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
  46.  
  47.      return GetOpenFileName (&ofn) ;
  48.      }
  49.  
  50. BOOL PopFileSaveDlg (HWND hwnd, PSTR pstrFileName, PSTR pstrTitleName)
  51.      {
  52.      ofn.hwndOwner         = hwnd ;
  53.      ofn.lpstrFile         = pstrFileName ;
  54.      ofn.lpstrFileTitle    = pstrTitleName ;
  55.      ofn.Flags             = OFN_OVERWRITEPROMPT ;
  56.  
  57.      return GetSaveFileName (&ofn) ;
  58.      }
  59.  
  60. static long PopFileLength (FILE *file)
  61.      {
  62.      int iCurrentPos, iFileLength ;
  63.  
  64.      iCurrentPos = ftell (file) ;
  65.  
  66.      fseek (file, 0, SEEK_END) ;
  67.  
  68.      iFileLength = ftell (file) ;
  69.  
  70.      fseek (file, iCurrentPos, SEEK_SET) ;
  71.  
  72.      return iFileLength ;
  73.      }
  74.  
  75. BOOL PopFileRead (HWND hwndEdit, PSTR pstrFileName)
  76.      {
  77.      FILE  *file ;
  78.      int    iLength ;
  79.      PSTR   pstrBuffer ;
  80.  
  81.      if (NULL == (file = fopen (pstrFileName, "rb")))
  82.           return FALSE ;
  83.  
  84.      iLength = PopFileLength (file) ;
  85.  
  86.      if (NULL == (pstrBuffer = (PSTR) malloc (iLength)))
  87.           {
  88.           fclose (file) ;
  89.           return FALSE ;
  90.           }
  91.  
  92.      fread (pstrBuffer, 1, iLength, file) ;
  93.      fclose (file) ;
  94.      pstrBuffer[iLength] = '\0' ;
  95.  
  96.      SetWindowText (hwndEdit, pstrBuffer) ;
  97.      free (pstrBuffer) ;
  98.  
  99.      return TRUE ;
  100.      }
  101.  
  102. BOOL PopFileWrite (HWND hwndEdit, PSTR pstrFileName)
  103.      {
  104.      FILE  *file ;
  105.      int    iLength ;
  106.      PSTR   pstrBuffer ;
  107.  
  108.      if (NULL == (file = fopen (pstrFileName, "wb")))
  109.           return FALSE ;
  110.  
  111.      iLength = GetWindowTextLength (hwndEdit) ;
  112.  
  113.      if (NULL == (pstrBuffer = (PSTR) malloc (iLength + 1)))
  114.           {
  115.           fclose (file) ;
  116.           return FALSE ;
  117.           }
  118.  
  119.      GetWindowText (hwndEdit, pstrBuffer, iLength + 1) ;
  120.  
  121.      if (iLength != (int) fwrite (pstrBuffer, 1, iLength, file))
  122.           {
  123.           fclose (file) ;
  124.           free (pstrBuffer) ;
  125.           return FALSE ;
  126.           }
  127.  
  128.      fclose (file) ;
  129.      free (pstrBuffer) ;
  130.  
  131.      return TRUE ;
  132.      }
  133.